<template>
	<div class="role-container">
		<el-form class="el_form" style="display: flex; justify-content: flex-start; padding: 10px 10px 10px 0">
			<el-button type="primary" @click="openAddRole">添加角色</el-button>
		</el-form>

		<table-components :tableLoading="tableLoading" :tableData="tableData" @currentChange="currentChange">
			<el-table-column prop="roleName" label="角色名称" align="center" />
			<el-table-column prop="createBy" label="创建人" align="center" />
			<el-table-column prop="createTime" label="创建时间" align="center" />
			<el-table-column label="操作" align="center">
				<template v-slot="scope">
					<el-button type="text" @click="openEdit(scope.row)">编辑</el-button>
					<el-button type="text" v-if="scope.row.roleStatus == 1" @click="enble(scope.row, 0)">启用</el-button>
					<el-button type="text" v-if="scope.row.roleStatus == 0" @click="enble(scope.row, 1)">禁用</el-button>
				</template>
			</el-table-column>
		</table-components>

		<addRole :isAdd="isAdd" :dialogVisible="addRoleVisible" :row="clickItem" @handleClose="addRoleClose"></addRole>
	</div>
</template>

<script setup lang="ts" name="role">
import { onMounted, reactive, ref } from 'vue';
import { ElMessage } from 'element-plus';
import { NextLoading } from '/@/utils/loading';
import TableComponents from '/@/components/tableComponents/index.vue';
import { getRoleList, roleEnble } from '/@/api/api';
import addRole from './dialog/addRole.vue';

const isAdd = ref(false);
const tableData = reactive({
	data: [],
	total: 0,
	page: 1,
	size: 20,
});
const tableLoading = ref(true);
const queryFrom = ref({});
const addRoleVisible = ref(false);
const clickItem = ref({});

// 页面加载时
onMounted(() => {
	NextLoading.done();
	getData();
});

async function getData() {
	tableLoading.value = true;
	let par = {
		page: tableData.page,
		size: tableData.size,
		...queryFrom.value,
	};

	let { code, data } = await getRoleList(par);
	if (code == 200) {
		tableData.data = data.list;
		tableData.total = data.total;
		tableLoading.value = false;
	}
}

function openAddRole(item) {
	isAdd.value = true;
	clickItem.value = item;
	addRoleVisible.value = true;
}

function addRoleClose() {
	isAdd.value = true;
	addRoleVisible.value = false;
	getData();
}

function openEdit(item: any) {
	isAdd.value = false;
	clickItem.value = item;
	addRoleVisible.value = true;
}

async function enble(item: any, status: number) {
	tableLoading.value = true;
	let par = {
		id: item.id,
		status: status,
	};

	let { code } = await roleEnble(par);
	tableLoading.value = false;
	if (code == 200) {
		ElMessage.success('操作成功');
		getData();
	}
}
</script>

<style scoped lang="scss">
@import url('/@/theme/page/page.scss');
.role-container{
	padding: 0 20px 20px 20px;
}
</style>
